package com.cpfmat.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.cpfmat.common.Contains;
import com.cpfmat.common.LogUtils;
import com.cpfmat.entity.SystemMenu;
import com.cpfmat.service.IArticleService;
import com.cpfmat.vo.CustomArticle;

@Controller
public class IndexController {
	@Autowired
	private IArticleService articleService;
	
	/**
	 * 
	 */
	@RequestMapping(path = "/index")
	public ModelAndView index(HttpServletRequest request){
		ModelAndView view = new ModelAndView("/new/homePage");
		view.addObject("type","index");
		if(request.getParameter("type") != null){
			view.addObject("type",request.getParameter("type"));	
		}
		
		String type = null;
		SystemMenu systemMenu = null;
		// 获取平台介绍
		type = "about";
		systemMenu = Contains.menuKeyMap.get(type);
		
		// 首页新闻获取3条
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(null, systemMenu.getSeqId(), null, 1, 3);

			if (page != null && page.getRecords() != null && page.getRecords().get(0) != null) {
				CustomArticle article = page.getRecords().get(0);
				
				view.addObject(type, article);
			}
		}
		
		// 获取首页新闻
		type = "index";
		systemMenu = Contains.menuKeyMap.get(type);
		
		// 首页新闻获取3条
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(null, systemMenu.getSeqId(), null, 1, 3);
			view.addObject(type,page);
		}
		
		// 获取平台动态
		type = "platform";
		systemMenu = Contains.menuKeyMap.get(type);
		
		// 平台动态获取9条
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(null, systemMenu.getSeqId(), null, 1, 9);
			view.addObject(type,page);
		}
		
		// 获取信息发布
		type = "news";
		systemMenu = Contains.menuKeyMap.get(type);
		
		// 信息发布获取12条
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(null, systemMenu.getSeqId(), null, 1, 12);
			view.addObject(type,page);
		}
		
		// 获取权威解读
		type = "authority";
		systemMenu = Contains.menuKeyMap.get(type);
		
		// 权威解读获取2条
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(null, systemMenu.getSeqId(), null, 1, 2);
			view.addObject(type,page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/manage/index")
	public ModelAndView manageIndex(){
		return new ModelAndView("/manage/index");
	}
	
	@RequestMapping(path = "/test/{chinese}")
	@ResponseBody
	public String testChinese(@PathVariable("chinese") String chinese){
		LogUtils.debug(chinese);
		return chinese;
	}
}
